<?php
/**
 * Created by PhpStorm.
 * User: vip79
 * Date: 2016/4/25
 * Time: 15:56
 */

namespace wap\controllers;

use services\models\Cart;
use services\models\CommentsRelation;
use Yii;
use services\models\SolrService;
use common\helper\StringHelper;

class GoodsListController extends CustomController
{

    //第一次页面加载
    public  function  actionIndex(){

        $request=\Yii::$app->getRequest()->getQueryParams();//获取请求参数
        $login_url = $this->login_url;
        $sort=isset($request['sort'])?$request['sort']:'DESC';
        $sort_type=isset($request['sort_type'])?$request['sort_type']:1;
        $page_num=isset($request['page_num'])?$request['page_num']:1;
        $cate_id=isset($request['cate_id'])?$request['cate_id']:1;
        $solr=new SolrService();
        $arr=$solr->WapSearch($sort_type,$sort,$page_num,$cate_id);
        $goods_info=array();
        $list_key = ['goods_id','goods_name','pic'];
        foreach($arr['goods_list'] as $k=>$v){
            $goods_info[$k]=\Yii::$app->redis_goods->hmget("goods_".$v,$list_key);
            $CommentsModel = new CommentsRelation();
            $goods_info[$k]['comment']=$CommentsModel->getFeedbackTotal($v)?$CommentsModel->getFeedbackTotal($v):0;
            $arr_sku=explode(",",\Yii::$app->redis_goods->hget("goods_".$v,'sku_id_str'));
            $list_key1=['sale_price','try_price','sale_status','type','price_type','default_sku'];
            if(!empty($arr_sku)){
                foreach($arr_sku as $key=>$value){
                    $sku_info=\Yii::$app->redis_goods->hmget("product_".$value,$list_key1);
                    //判断是否是已经上架的默认销售商品
                    if($sku_info['sale_status']&&$sku_info['default_sku']&&$sku_info['type']==2){
                        if($sku_info['price_type']==1){
                            $goods_info[$k]['sale_price']=$sku_info['try_price'];
                            $goods_info[$k]['sale_price_type']=1;//试豆显示
                        }else{
                            $goods_info[$k]['sale_price']=$sku_info['sale_price'];
                            $goods_info[$k]['sale_price_type']=0;//价格显示
                        }
                    }
                    //判断是否是已经上架的默认试用商品
                    if($sku_info['sale_status']&&$sku_info['default_sku']&&$sku_info['type']==1){
                        if($sku_info['price_type']==1){
                            $goods_info[$k]['try_price']=$sku_info['try_price'];
                            $goods_info[$k]['try_price_type']=1;//试豆显示
                        }else{
                            $goods_info[$k]['try_price']=$sku_info['sale_price'];
                            $goods_info[$k]['try_price_type']=0;//价格显示
                        }
                    }



                }
            }
            $default_sku=\Yii::$app->redis_goods->hmget("goods_".$v,['default_sku']);//默认取销售商品的sku如果销售商品未上架则取试用商品的默认sku
            $goods_info[$k]['default_sku']=$default_sku['default_sku'];
            $goods_info[$k]['goods_name']=StringHelper::subtext( $goods_info[$k]['goods_name'],22);

        }
        $cart_num=Cart::getUserCartNum($this->bestry_uid);
        return $this->render(
            'list',['goods_info'=>$goods_info,
            'rorder'=>($sort=="DESC"?"ASC":'DESC'),
            'sort_type'=>$sort_type,
            'cart_num'=>$cart_num,
             'login_url'=>$login_url,
            'bestry_uid' => $this->bestry_uid,
        ]);
    }

    //异步调用
    public  function  actionAjax_page(){


        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $request= Yii::$app->request->post();

        $sort=isset($request['sort'])?$request['sort']:'DESC';
        $sort_type=isset($request['sort_type'])?$request['sort_type']:1;
        $page_num=isset($request['page_num'])?$request['page_num']:1;
        $cate_id=isset($request['cate_id'])?$request['cate_id']:1;
        $solr=new SolrService();

        $arr=$solr->WapSearch($sort_type,$sort,$page_num,$cate_id);
        $goods_info=array();
        $list_key = ['goods_id','goods_name','pic'];

        foreach($arr['goods_list'] as $k=>$v) {
            $goods_info[$k] = \Yii::$app->redis_goods->hmget("goods_" . $v, $list_key);
            $CommentsModel = new CommentsRelation();
            $goods_info[$k]['comment'] = $CommentsModel->getFeedbackTotal($v) ? $CommentsModel->getFeedbackTotal($v) : 0;
            $arr_sku = explode(",", \Yii::$app->redis_goods->hget("goods_" . $v, 'sku_id_str'));
            $list_key1 = ['sale_price', 'try_price', 'sale_status', 'type', 'price_type', 'default_sku'];
            if(!empty($arr_sku)){
                foreach($arr_sku as $key=>$value){
                    $sku_info=\Yii::$app->redis_goods->hmget("product_".$value,$list_key1);
                    //判断是否是已经上架的默认销售商品
                    if($sku_info['sale_status']&&$sku_info['default_sku']&&$sku_info['type']==2){
                        if($sku_info['price_type']==0){
                            $goods_info[$k]['sale_price']=$sku_info['sale_price'];
                            $goods_info[$k]['sale_price_type']=0;//试豆显示
                        }else{
                            $goods_info[$k]['sale_price']=$sku_info['sale_price'];
                            $goods_info[$k]['sale_price_type']=0;//价格显示
                        }
                    }
                    //判断是否是已经上架的默认试用商品
                    if($sku_info['sale_status']&&$sku_info['default_sku']&&$sku_info['type']==1){
                        if($sku_info['price_type']==1){
                            $goods_info[$k]['try_price']=$sku_info['try_price'];
                            $goods_info[$k]['try_price_type']=1;//试豆显示
                        }else{
                            $goods_info[$k]['try_price']=$sku_info['sale_price'];
                            $goods_info[$k]['try_price_type']=0;//价格显示
                        }
                    }



                }
            }
            $default_sku=\Yii::$app->redis_goods->hmget("goods_".$v,['default_sku']);//默认取销售商品的sku如果销售商品未上架则取试用商品的默认sku
            $goods_info[$k]['default_sku']=$default_sku['default_sku'];
            $goods_info[$k]['goods_name']=StringHelper::subtext( $goods_info[$k]['goods_name'],22);
        }
          $flag = false;
          if(!empty($goods_info)){
              $html=$this->renderPartial("ajaxpage",['goods_info'=>$goods_info]);
              $flag = true;
          }else{
               $html="";
               $flag=true;
          }
          $return_arr['flag'] = $flag;
          $return_arr['text'] = $html;
           return $return_arr;




    }

}